package com.fancon.android.emoji;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.RadioButton;

import com.fancon.android.emoji.EmojiKeyBoard.EmojiKeyBoardListener;
import com.fancon.android.emoji.keyboard.BetterPopupWindow;
import com.fancon.android.emoji.widget.RichEditText;

public class EmojiDockPanel extends BetterPopupWindow implements
		OnClickListener {
	public interface EmojiDockListener {
		public void onItemSelected(View v);

		public void onClose();
	}

	private EmojiDockListener listener;
	private RadioButton radioSmiley, radioBell, radioVehicle, radioFlower,
			radioNumber;
	protected RichEditText richEditText;

	public EmojiDockPanel(View anchor, RichEditText richEditText) {
		super(anchor);
		this.richEditText = richEditText;
		// TODO Auto-generated constructor stub
	}

	public EmojiDockPanel(View anchor, RichEditText richEditText,
			EmojiDockListener listener) {
		super(anchor);
		// TODO Auto-generated constructor stub
		this.richEditText = richEditText;
		this.listener = listener;
	}

	protected void onCreate() {
		// inflate layout
		LayoutInflater inflater = (LayoutInflater) this.anchor.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.emoji_panel,
				null);
		radioSmiley = (RadioButton) root.findViewById(R.id.radioSmiley);
		radioBell = (RadioButton) root.findViewById(R.id.radioBell);
		radioVehicle = (RadioButton) root.findViewById(R.id.radioVehicle);
		radioFlower = (RadioButton) root.findViewById(R.id.radioFlower);
		radioNumber = (RadioButton) root.findViewById(R.id.radioNumber);

		radioSmiley.setOnClickListener(this);
		radioBell.setOnClickListener(this);
		radioVehicle.setOnClickListener(this);
		radioFlower.setOnClickListener(this);
		radioNumber.setOnClickListener(this);

		this.setContentView(root);
	}

	public void showPanel(int keyboardHeight) {
		this.preShow();
		this.window.setFocusable(false);
		this.window.setTouchable(true);
		this.window.setOutsideTouchable(false);
		this.window.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
		
		this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
		int[] location = new int[2];
		this.anchor.getLocationOnScreen(location);
		this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		// int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
		int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
		this.window.showAtLocation(this.anchor, Gravity.TOP, 0, screenHeight
				- keyboardHeight - this.root.getMeasuredHeight());
	}

	public EmojiDockListener getDockListener() {
		return listener;
	}

	public void getDockListener(EmojiDockListener listener) {
		this.listener = listener;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v instanceof RadioButton) {
			if (v == radioSmiley) {
				openEmojiKeyBoard(EmojiUtil.SMILEY, v);
			} else if (v == radioBell) {
				openEmojiKeyBoard(EmojiUtil.BELL, v);
			} else if (v == radioVehicle) {
				openEmojiKeyBoard(EmojiUtil.VEHICLE, v);
			} else if (v == radioFlower) {
				openEmojiKeyBoard(EmojiUtil.FLOWER, v);
			} else if (v == radioNumber) {
				openEmojiKeyBoard(EmojiUtil.NUMBER, v);
			}
			if (listener != null) {
				listener.onItemSelected(v);
			}
		}
	}

	public void openEmojiKeyBoard(int category, View v) {
		// For multi input
		View focus = ((Activity) (richEditText.getContext())).getCurrentFocus();
		if (focus instanceof RichEditText) {
			final EmojiKeyBoardListener listener = new EmojiKeyBoardListener() {
				@Override
				public void onEmojiSelected(View view) {
					// TODO Auto-generated method stub
					String code = ((Emoji) view).getCode();
					Spanned cs = Html
							.fromHtml("<img src='" + code + "'/>", richEditText
									.getmEmojiUtil().getImageGetter(), null);
					// For multi input
					RichEditText currentFocus = (RichEditText) ((Activity) (richEditText
							.getContext())).getCurrentFocus();
					// int start = richEditText.getSelectionStart();
					int start = currentFocus.getSelectionStart();
					// EmojiUtil.insertSpannedTextToCurrentPosition(cs,
					// richEditText,
					// start, start);
					EmojiUtil.insertSpannedTextToCurrentPosition(cs,
							currentFocus, start, start);
				}

				@Override
				public void afterClose(View v) {
					// TODO Auto-generated method stub
				}
			};
			richEditText.hideKeyBoard();
			dismiss();
			EmojiKeyBoard dw = new EmojiKeyBoard(v, false, category, listener);
			dw.showLikeKeyBoard();
		}else{
			//Toast.makeText(v.getContext(), "this field is not emoji keyboard", 1000).show();
		}
	}
}
